package com.example.annotation.validate;

import com.example.enums.validate.CaseMode;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/9/22 14:59
 */
public class CheckCaseValidator  implements ConstraintValidator<CheckCase, String> {
    private CaseMode caseMode;
    @Override
    public void initialize(CheckCase checkCase) {
        this.caseMode = checkCase.value();
    }
    @Override
    public boolean isValid(String s,ConstraintValidatorContext constraintValidatorContext) {
        if (s == null) {
            return true;
        }
        if (caseMode == CaseMode.UPPER) {
            return s.equals(s.toUpperCase());
        } else {
            return s.equals(s.toLowerCase());
        }
    }
}
